package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.service.CarService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "AddCarServlet", urlPatterns = {"/auth/addCar"})
public class AddCarServlet extends HttpServlet {

    @EJB
    private CarService carServ;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect(getServletContext().getContextPath()+ "/jsp/auth/addCar.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String name = request.getParameter("name");
       String brand = request.getParameter("brand");
       Date date =null;
       Long carId= Long.parseLong(request.getParameter("carId"));
       
       User userLogged =(User)request.getSession().getAttribute("userlogged");
       
       // Date parsing
       String datestr = request.getParameter("initialEntryIntoService");
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(datestr);
        } catch (ParseException ex) {
            Logger.getLogger(AddCarServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Creation of test cars
        List<Car> carInDb = carServ.processGetAllCar();
        if (carInDb.isEmpty()){
            String id1 = "1";
            String id2 = "2";
            Car car1= new Car();
            car1.setActualLatitude("1234");
            car1.setActualLongitude("1234");
            car1.setId(Long.parseLong(id1));
            carServ.processAddCar(car1);
            Car car2= new Car();
            car2.setActualLatitude("4321");
            car2.setActualLongitude("4321");
            car2.setId(Long.parseLong(id2));
            carServ.processAddCar(car2);
        }
        
        Car car = carServ.processGetCarById(carId);
        

        if (car==null){
            System.out.println("Je passe pas le test");
            response.sendRedirect(request.getServletContext().getContextPath()+"/jsp/auth/addCar.jsp");
            request.getSession().setAttribute("event", "0");
            request.getSession().setAttribute("message", "Car does not exist!");
        }
        else{
            User userExist = car.getUser();
            if(userExist != null){
                response.sendRedirect(request.getServletContext().getContextPath()+"/jsp/auth/addCar.jsp");
                request.getSession().setAttribute("event", "0");
                request.getSession().setAttribute("message", "Car is already linked to a user");
            }
            else{
            car.setUser(userLogged);
            car.setInitialEntryIntoService(date);
            car.setBrand(brand);
            car.setName(name);
            
            carServ.processUpdateCar(car);
            request.getSession().setAttribute("event", "1");
            request.getSession().setAttribute("message", "Car added!");
            response.sendRedirect(getServletContext().getContextPath() + "/auth/listCar");
            }
        }
    }
}
